package com.dd.cloud.user.service.paynotice.promode.notice;

import com.dd.cloud.user.entity.order.OrderPay;
import com.dd.cloud.user.service.doctor.IDoctorEquityService;
import com.dd.cloud.user.service.doctor.IDoctorIntegralService;
import com.dd.cloud.user.service.doctor.IDoctorOrderIncomeDetailService;
import com.dd.cloud.user.service.paynotice.promode.NoticeService;
import com.dd.cloud.user.service.paynotice.promode.NoticeType;
import com.dd.cloud.user.service.pres.IPresPrescriptionService;
import com.zk.pay.constants.CS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

/**
 * 退款处方产品回调
 */
@NoticeType(source = CS.NOTICE_SOURCE.REFUND, proType = CS.NOTICE_CHANNEL.PRE)
public class PreRefundNoticeService implements NoticeService {
    @Autowired
    private IPresPrescriptionService presPrescriptionService;
    @Autowired
    private IDoctorOrderIncomeDetailService doctorOrderIncomeDetailService;
    @Autowired
    private IDoctorIntegralService doctorIntegralService;

    @Autowired
    private IDoctorEquityService doctorEquityService;


    /**
     * 退款处理逻辑
     *
     * @param request
     * @param orderPay 订单对象
     * @param flowNo   流水编号
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @Async
    public void notice(HttpServletRequest request, OrderPay orderPay, String flowNo) {
        //异步方法, 处理结果和异常不能影响回调的返回
        /**
         * 处方退款回调
         */
        presPrescriptionService.refundPresCallback(orderPay);
        /**
         * 收入明细退款消息
         */
        doctorOrderIncomeDetailService.addDoctorOrderIncomeDetail(orderPay);

        /**
         * 退款回收积分
         */
        doctorIntegralService.refundIntegral(orderPay);

        /**
         * 回收赠送时间
         */
        doctorEquityService.refundTime(orderPay);
    }
}
